//
//  MDHttpService.m
//  jewelryMD
//
//  Created by 洪闽程 on 2018/7/7.
//  Copyright © 2018年 洪闽程. All rights reserved.
//

#import "MDHttpService.h"
#import <AFNetworking.h>
#import "UIImage+scale.h"
@implementation MDHttpService
//设置单例
+(MDHttpService *)shareHttpService
{
    static MDHttpService *httpService;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpService = [[[self class] alloc] init];
    });
    return httpService;
}

//get请求
-(void)getUrlStr:(NSString *)urlstr andparam:(NSDictionary *)param andsuccess:(successBlock)successblock andfailed:(failBlock)failblock {
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    [manager GET:urlstr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
        NSDictionary * infodic =[NSDictionary dictionaryWithDictionary:responseObject];
        successblock(infodic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
        MError *merror = nil;
        if (error.code == -1009) {
            merror = [[MError alloc] initWithCode:error.code statsMsg:@"当前网络不可用哦，请查看网络设置"];
        }else {
            merror = [[MError alloc] initWithCode:error.code statsMsg:@"网络故障"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            failblock(merror);
        });
    }];
}
//post请求
-(void)postUrlStr:(NSString *)urlstr andparam:(NSDictionary *)param andsuccess:(successBlock)successblock andfailed:(failBlock)failblock {
    [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 5;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;
    manager.securityPolicy = securityPolicy;
    [manager POST:urlstr parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        NSDictionary * infodic =[NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        successblock(infodic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        MError *merror = nil;
        if (error.code == -1009) {
            merror = [[MError alloc] initWithCode:error.code statsMsg:@"当前网络不可用哦，请查看网络设置"];
        }else {
            merror = [[MError alloc] initWithCode:error.code statsMsg:@"网络故障"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            failblock(merror);
        });
    }];
    
}

@end
